<?php

class LaptopModel
{
    /**
     * Every model needs a database connection, passed to the model
     * @param object $db A PDO database connection
     * 
     * fetchAll() is the PDO method that gets all result rows, here in object-style because we defined this in
	 * libs/controller.php! If you prefer to get an associative array as the result, then do
	 * $query->fetchAll(PDO::FETCH_ASSOC); or change libs/controller.php's PDO options to
	 * $options = array(PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC ...
     */
		
	
    function __construct($db) {
        try {
            $this->db = $db;
        } catch (PDOException $e) {
            exit('Database connection could not be established.');
        }
    }

    public function getAllLaptops(){
    	
        $sql = "SELECT * FROM tbl_laptops";
        $query = $this->db->prepare($sql);
        $query->execute();
        
        return $query->fetchAll();
    }
    
    public function getPaginatedLaptops($start,$limit){
    	
    	$sql = "SELECT * FROM tbl_laptops LIMIT ".$start.",".$limit;
    	//$sql = "SELECT * FROM tbl_laptops LIMIT :start,:limit";
    	$query = $this->db->prepare($sql);
    	//$query->bindParam(':start',$start);
    	//$query->bindParam(':limit',$limit);
    	$query->execute();
    
    	return $query->fetchAll();
    }
    
    public function getLaptopById($id){
    	
        $sql = "SELECT * FROM tbl_laptops WHERE ID= :id";
        $query = $this->db->prepare($sql);
        $query->execute(array(':id' => $id));

        return $query->fetchAll();
    }
    
    public function countSold(){
    	
    	$sql = "SELECT SUM(SOLDQTY) AS SOLD FROM tbl_laptops";
    	$query = $this->db->prepare($sql);
    	$query->execute();

    	return $query->fetchAll();
    }
    
    public function countLaptops(){
    	 
    	$sql = "SELECT COUNT(*) AS LAPCOUNT FROM tbl_laptops";
    	$query = $this->db->prepare($sql);
    	$query->execute();
    
    	return $query->fetchAll();
    }

    public function getLatestLaptopList(){
    	 
    	$sql = "SELECT * FROM tbl_laptops ORDER BY DATEADDED DESC LIMIT 6";
    	$query = $this->db->prepare($sql);
    	$query->execute();
    
    	return $query->fetchAll();
    }
    
    public function getLaptopListByBrand($brand){
    
    	$sql = "SELECT * FROM tbl_laptops WHERE BRAND= :brand";
    	$query = $this->db->prepare($sql);
    	$query->execute(array(':brand' => $brand));
    
    	return $query->fetchAll();
    }
    
    public function getPaginatedLaptopListByBrand($brand,$start,$limit){
    
    	$sql = "SELECT * FROM tbl_laptops WHERE BRAND= :brand LIMIT ".$start.",".$limit;
    	$query = $this->db->prepare($sql);
    	$query->execute(array(':brand' => $brand));
    
    	return $query->fetchAll();
    }
    
    public function countLaptopsByBrand($brand){
    
    	$sql = "SELECT COUNT(*) AS LAPCOUNT FROM tbl_laptops WHERE BRAND= :brand";
    	$query = $this->db->prepare($sql);
    	$query->execute(array(':brand' => $brand));
    
    	return $query->fetchAll();
    }
    
    public function getLaptopListByModel($model){
    
    	$model = "%".$model."%";
    	
    	$sql = "SELECT * FROM tbl_laptops WHERE MODEL LIKE :model";
    	$query = $this->db->prepare($sql);
    	$query->execute(array(':model' => $model));
    
    	return $query->fetchAll();
    }
    
    public function getPaginatedLaptopListByModel($model,$start,$limit){
    
    	$model = "%".$model."%";
    	
    	$sql = "SELECT * FROM tbl_laptops WHERE MODEL LIKE :model LIMIT ".$start.",".$limit;
    	$query = $this->db->prepare($sql);
    	$query->execute(array(':model' => $model));
    
    	return $query->fetchAll();
    }
    
    public function countLaptopsByModel($model){
    	
    	$model = "%".$model."%";
    	
    	$sql = "SELECT COUNT(*) AS LAPCOUNT FROM tbl_laptops WHERE MODEL LIKE :model";
    	$query = $this->db->prepare($sql);
    	$query->execute(array(':model' => $model));
    
    	return $query->fetchAll();
    }
    
    public function searchPaginatedLaptopList($keyword,$start,$limit){
    
    	$keyword = "%".$keyword."%";
    	
    	$sql = "SELECT * FROM tbl_laptops WHERE BRAND LIKE :keyword OR TYPE LIKE :keyword OR MODEL LIKE :keyword OR CPU LIKE :keyword OR OS LIKE :keyword LIMIT ".$start.",".$limit;
    	$query = $this->db->prepare($sql);
    	$query->execute(array(':keyword' => $keyword));
    
    	return $query->fetchAll();
    }
    
    public function countSearchLaptops($keyword){
    
    	$keyword = "%".$keyword."%";
    	
    	$sql = "SELECT COUNT(*) AS LAPCOUNT FROM tbl_laptops WHERE BRAND LIKE :keyword OR TYPE LIKE :keyword OR MODEL LIKE :keyword OR CPU LIKE :keyword OR OS LIKE :keyword";
    	$query = $this->db->prepare($sql);
    	$query->execute(array(':keyword' => $keyword));
    
    	return $query->fetchAll();
    }
}
